package ca.concordia.soen6461.wichf.navigation.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.controller.View;
import ca.concordia.soen6461.wichf.dao.PatientCartDao;
import ca.concordia.soen6461.wichf.entity.PatientCart;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.navigation.event.CheckoutEvent;
import ca.concordia.soen6461.wichf.navigation.view.CartView.Appointment;


public class CheckoutView implements View<CheckoutView.Bean> {

	public static class Bean extends CartView.Bean {
		
		private int total;

		public int getTotal() {
			return total;
		}

		public void setTotal(int total) {
			this.total = total;
		}
		
	}

	private PatientCartDao patientCartDao;

	public CheckoutView(PatientCartDao patientCartDao) {
		this.patientCartDao = patientCartDao;
	}

	public Bean render(Event event) {
		DateFormat format = new SimpleDateFormat("MMM dd - HH:mm");
		Bean bean = new Bean();
		User user = event.getSession().get(User.class);
		CheckoutEvent checkoutEvent = (CheckoutEvent) event;
		PatientCart cart = patientCartDao.get(user);
		if (cart != null) {
			int item = 0;
			for (ScheduledEvent appointment : cart.getAppointments()) {
				if (checkoutEvent.getSelectedAppointments().contains(item)) {
					Appointment a = new Appointment();
					a.setItem(item);
					a.setDate(format.format(appointment.getFrom()));
					a.setPrice(appointment.getType().getPrice());
					a.setType(appointment.getType().getDescription());
					bean.getAppointments().add(a);
					bean.setTotal(bean.getTotal() + a.getPrice());
				}
				item++;
			}
		}
		return bean;
	}

}
